/**
 * User: Obi
 * Date: 02.08.11
 * Time: 23:20
 */
package com.jediflasher.game_client.view {
import com.jediflasher.game_client.data.LoginData;
import com.jediflasher.game_client.events.model.LoginDataEvent;
import com.jediflasher.game_client.events.view.LoginViewEvent;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;

public class LoginView extends BaseView {

	public function LoginView(data:LoginData) {
		super(data);
	}

	/**
	 * @private
	 */
	private var _panel:DisplayObjectContainer;

	/**
	 * @private
	 */
	private var _loginInput:TextField;

	/**
	 * @private
	 */
	private var _passwordInput:TextField;

	/**
	 * @private
	 */
	private var _okButton:DisplayObject;

	override public function render(event:Event = null):Boolean {
		if(!super.render(event)) return false;

		if(this._panel) super.removeChild(this._panel);

		this._data.addEventListener(LoginDataEvent.LOGIN_ERROR, this.handler_loginError);

		this._panel = new loginPanel();
		this._loginInput = this._panel.getChildByName("loginInput") as TextField;
		this._passwordInput = this._panel.getChildByName("passwordInput") as TextField;
		this._okButton = this._panel.getChildByName("okBtn");
		this._okButton.addEventListener(MouseEvent.CLICK, this.handler_loginClick);

		super.addChild(this._panel);

		return true;
	}

	override public function clear(event:Event = null):void {
		super.clear(event);

		if(this._panel) super.removeChild(this._panel);
		if(this._okButton) this._okButton.removeEventListener(MouseEvent.CLICK, this.handler_loginClick);
	}

	/**
	 * @private
	 */
	private function handler_loginClick(event:MouseEvent):void {
		if(this._loginInput.text.length < 2) return;
		if(this._passwordInput.text.length < 2) return;

		super.dispatchEvent(new LoginViewEvent(LoginViewEvent.LOGIN, this._loginInput.text, this._passwordInput.text));
	}

	/**
	 * @private
	 */
	private function handler_loginError(event:LoginDataEvent):void {
		trace("Login error!");
	}
}
}
